package com.lib76.socket;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.commons.lang.StringUtils;

import com.lib76.socket.impl.ByteTcpSocketObject;
import com.lib76.socket.impl.ByteUdpSocketObject;
import com.lib76.socket.impl.StringTcpSocketObject;
import com.lib76.socket.model.SocketInfo;
import com.lib76.socket.model.protocol.SocketProtocolGroup;

/**
 * 通讯服务
 * @author 张浩
 * @version 1.0.2
 */
public class CommunicationServer {
	
	private final static ConcurrentMap<String, SocketObject> SOCKET_OBJECT_STORE = new ConcurrentHashMap<String, SocketObject>();

	/**
	 * 报文协议初始化
	 * @return
	 */
	public static boolean messageProtocolInitial(InputStream in,Properties prop){
		try{
			SocketProtocolGroup protocolGroup = ConfigXmlResolver.analyzeMessageProtocol(in, prop);
			return SocketMessageProtocolManager.addSocketProtocolGroup(protocolGroup);
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Socket初始化
	 * @param cfgPath
	 * @param prop
	 * @throws IOException
	 */
	public static boolean socketInitial(InputStream in,Properties prop){
		try{
			return ConfigXmlResolver.analyzeXmlConfig(in,prop);
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 创建并保存Socket通讯
	 * @param info
	 * @return
	 */
	public static boolean createAndSaveSocketEntity(SocketInfo info){
		if(info != null && StringUtils.isNotBlank(info.getId())){
			if(StringUtils.isNotBlank(info.getMessageProtocolVersion())){
				SocketProtocolGroup protocolGroup = SocketMessageProtocolManager.getSocketProtocolGroup(info.getMessageProtocolVersion());
				if(protocolGroup != null){
					SocketObject entity = createSocketObject(info,protocolGroup.getType());
					if(entity != null){
						SOCKET_OBJECT_STORE.put(info.getId(), entity);
						return true;
					}
				}
			}
			if(StringUtils.isNotBlank(info.getMessageProtocolType())){
				SocketObject entity = createSocketObject(info,info.getMessageProtocolType());
				SOCKET_OBJECT_STORE.put(info.getId(), entity);
				return true;
			}
		}
		return false;
	}
	
	private static SocketObject createSocketObject(SocketInfo info,String messageProtocolType){
		SocketObject entity = null;
		if(Constants.MSG_PROTOCOL_TYPE_BYTE.equals(messageProtocolType)){
			if(SocketTypeEnum.tcp.equals(info.getType())){
				entity = new ByteTcpSocketObject(info);
			}else if(SocketTypeEnum.udp.equals(info.getType())){
				entity = new ByteUdpSocketObject(info);
			}
		}else if(Constants.MSG_PROTOCOL_TYPE_JSON.equals(messageProtocolType)
			|| Constants.MSG_PROTOCOL_TYPE_SPACE.equals(messageProtocolType)){
			if(SocketTypeEnum.tcp.equals(info.getType())){
				entity = new StringTcpSocketObject(info);
			}else if(SocketTypeEnum.udp.equals(info.getType())){
				//entity = new StringUdpSocketObject(info);
			}
		}else{
			//TODO 提示错误
		}
		return entity;
	}

	public static SocketObject getSocketObject(String id){
		return SOCKET_OBJECT_STORE.get(id);
	}
}
